﻿using System;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Validity.Test.Constraints.StringConstraints
{
  [TestClass]
  public class MatchesPatternConstraint
    : ConstraintTestBase<ObjectStub>
  {
    public class Validator
      : ValidatorBase<ObjectStub>
    {
      public Validator()
      {
        DefineRulesFor(ctx => ctx.MyStringProperty1).MatchesPattern(@"\w+");
        DefineRulesFor(ctx => ctx.MyStringProperty2).MatchesPattern(@"\w+", RegexOptions.None);
      }
    }

    public MatchesPatternConstraint() : base(new Validator(), 2) { }

    [TestMethod]
    public void TestConstraintVerified()
    {
      ValidateConstraintVerfied(CreateContext("Word"));
    }

    [TestMethod]
    public void TestConstraintViolated()
    {
      const String ruleViolation = @"Must match the pattern '\w+'";

      ValidateConstraintViolated(CreateContext(null), ruleViolation);
      ValidateConstraintViolated(CreateContext(String.Empty), ruleViolation);
      ValidateConstraintViolated(CreateContext("."), ruleViolation);
      ValidateConstraintViolated(CreateContext(" "), ruleViolation);
    }

    public ObjectStub CreateContext(String value)
    {
      var result = new ObjectStub();

      result.MyStringProperty1 = value;
      result.MyStringProperty2 = value;

      return result;
    }
  }
}
